package com.iflytek.service.Impl;

import com.iflytek.bean.Emp;
import com.iflytek.dao.EmpDao;
import com.iflytek.dao.Impl.EmpDaoImpl;
import com.iflytek.service.EmpService;

import java.util.List;
import java.util.Scanner;


public class EmpServiceImpl implements EmpService {
    EmpDao dao =  new EmpDaoImpl();
    static Scanner sc=new Scanner(System.in);
    String regex1 = "^\\d{4}$";
    String regex2 = "^[0-9]+(.[0-9]{2})?$";
    String regex3 = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$";
    int sr1;
    Double sr2;
    String sr3;

    /**
     * 获取所有员工信息的方法
     * @return
     */
    @Override
    public List<Emp> getAllEmp() {
        return dao.getAllEmp();
    }

    @Override
    public void addEmp(Emp emp) {
        for (int i=0;i<6;i++){
            switch (i) {
                case 0:
                    do {
                        System.out.println("请输入四位数的员工编号：");
                        sr3= sc.next();
                        if (sr3.matches(regex1)){
                            sr1 = Integer.parseInt(sr3);
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setEmpno(sr1);
                    break;
                case 1:
                    do {
                        System.out.println("请输入员工姓名：");
                        sr3 = sc.next();
                        if (sr3.matches(regex3)){
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setEname(sr3);
                    break;
                case 2:
                    do {
                        System.out.println("请输入员工职务：");
                        sr3 = sc.next();
                        if (sr3.matches(regex3)){
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setJob(sr3);
                    break;
                case 3:
                    do {
                        System.out.println("请输入薪资(正数)：");
                        sr3 = sc.next();
                        if (sr3.matches(regex2)){
                            sr2 = Double.parseDouble(sr3);
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setSal(sr2);
                    break;
                case 4:
                    do {
                        System.out.println("请输入奖金(正数)：");
                        sr3 = sc.next();
                        if (sr3.matches(regex2)){
                            sr2 = Double.parseDouble(sr3);
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setComm(sr2);
                    break;
                case 5:
                    do {
                        System.out.println("请输入部门编号：");
                        sr3 = sc.next();
                        if (sr3.equals("100")||sr3.equals("101")||sr3.equals("102")){
                            sr1 = Integer.parseInt(sr3);
                            break;
                        }else {
                            System.out.println("输入错误，请重新输入！！！\n===================");
                        }
                    } while (true);
                    emp.setDeptno(sr1);
                    break;
            }
        }
        dao.addEmp(emp);
    }

    /**
     * 修改员工信息
     * @param empno
     */
    @Override
    public void upDate(int empno) {
        System.out.println("1.姓名\n2.职位\n3.薪资\n4.奖金\n5.部门编号\n选择你需要修改的信息: ");
        String updateChoose = null;
        Object object2 = null;
        updateChoose = sc.next();
        switch (updateChoose){
            case "1":
                updateChoose="ename";
                do {
                    System.out.println("请输入修改后的信息: ");
                    sr3 = sc.next();
                    if (sr3.matches(regex3)){
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入!!!\n============");
                    }
                }while (true);
                object2=sr3;
                break;
            case "2":
                updateChoose="job";
                do {
                    System.out.println("请输入修改后的信息: ");
                    sr3 = sc.next();
                    if (sr3.matches(regex3)){
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入!!!\n============");
                    }
                }while (true);
                object2=sr3;
                break;
            case "3":
                updateChoose="sal";
                do {
                    System.out.println("请输入修改后的信息: ");
                    sr3 = sc.next();
                    if (sr3.matches(regex2)){
                        sr2 = Double.parseDouble(sr3);
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入!!!\n============");
                    }
                }while (true);
                object2=sr2;
                break;
            case "4":
                updateChoose="comm";
                do {
                    System.out.println("请输入修改后的信息: ");
                    sr3 = sc.next();
                    if (sr3.matches(regex2)){
                        sr2 = Double.parseDouble(sr3);
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入!!!\n============");
                    }
                }while (true);
                object2=sr2;
                break;
            case "5":
                updateChoose="deptno";
                do {
                    System.out.println("请输入修改后的信息: ");
                    sr3 = sc.next();
                    if (sr3.equals("100")||sr3.equals("101")||sr3.equals("102")){
                        sr1 = Integer.parseInt(sr3);
                        break;
                    }else {
                        System.out.println("输入错误,请重新输入!!!\n============");
                    }
                }while (true);
                object2=sr1;
                break;
            default:
                System.out.println("输入错误,请重新输入!!!\n=============");
        }
        dao.upDate(empno,object2,updateChoose);
    }

    @Override
    public void delEmpById(int empno) {
        dao.delEmpById(empno);
    }

    @Override
    public List<Emp> selectLikeName(String ename) {
        List<Emp> empList=dao.selectLikeName(ename);
        return empList;
    }

}
